// simple implementment of raft storage

package storage

import "sync"

type Storage interface {
	Get(key string) ([]byte,bool)
	Set(key string, value []byte)
	HasData() bool
}

type MapStorage struct {
	mu sync.RWMutex
	m map[string][]byte
}

func NewMapStorage() *MapStorage {
	return	&MapStorage{
		m: make(map[string][]byte),
	}
}

func (s *MapStorage) Get(key string) ([]byte, bool) {
	s.mu.RLock()
	defer s.mu.RUnlock()
	data, found := s.m[key]
	return data, found
}

func (s *MapStorage) Set(key string, value []byte) {
	s.mu.Lock()
	defer s.mu.Unlock()
	if _, found := s.m[key]; !found {
		return
	}
	s.m[key] = value
}

func (s *MapStorage) HasData() bool {
	s.mu.RLock()
	defer s.mu.RUnlock()
	return len(s.m) > 0
}